﻿Public Class WorkOrderExplorer
    Inherits ExplorerTreeList

    Private aMrpHeader As PLBase
    Private aMrpRevision As PLBase
    Public MRPID As Guid
    Public MRPRevID As Guid

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Sub New()
        MyBase.New()
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        ' Memory management / Help the GC
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '
        If aMrpHeader IsNot Nothing Then
            aMrpHeader.Dispose()
            aMrpHeader = Nothing
        End If
        '
        If aMrpRevision IsNot Nothing Then
            aMrpRevision.Dispose()
            aMrpRevision = Nothing
        End If
        '
        Dim guidParent As Guid = Nothing
        Dim guidEnfant As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        Dim frmCaption As String = Nothing

        Select Case FocusedNodeLevel
            Case 0
                frmCaption = "Work Order List"
                ShowAll()
            Case 1
                If NodeDataRow.Table.Columns.Contains("Code") Then
                    frmCaption = String.Format("{0}", NodeDataRow("Code"))
                Else
                    frmCaption = "Code"
                End If

                MRPID = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
                ShowMRPHeader(guidEnfant)
            Case 2
                If NodeDataRow.Table.Columns.Contains("Code") Then
                    frmCaption = String.Format("{0}", NodeDataRow("Code"))
                Else
                    frmCaption = "Code"
                End If

                MRPID = DirectCast(NodeDataRow(Me.ParentFieldName), Guid)
                MRPRevID = guidEnfant
                ShowMRPRevision(guidEnfant)
            Case Else
        End Select

        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ShowAll()
        Me.Cursor = Cursors.WaitCursor
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = MyBase.PL, .SecureGrid = True, .Name = "WO List", .FormCtrlCD = MyBase.PanelName}
        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ShowMRPHeader(ByVal woID As System.Guid)
        Me.Cursor = Cursors.WaitCursor
        aMrpHeader = PLBase.CreateInstanceOf(PLTypeOf.MRPHeader_PL, "MRPHeader_Express")
        'Scoper nouveau PL en haut en haut        
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aMrpHeader, .SecureGrid = True, .Name = "WO List", .FormCtrlCD = MyBase.PanelName}
        aMrpHeader.SQLWhere = String.Format("MRPID = '{0}'", woID)
        aGridControl.ExpandFirstMasterRow = True
        AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateRow
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateRow
        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewRow
        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewRow
        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ShowMRPRevision(ByVal mrpRevID As System.Guid)
        Me.Cursor = Cursors.WaitCursor
        aMrpRevision = PLBase.CreateInstanceOf(PLTypeOf.MRPRevision_PL, "MRPRevision_Express")
        aMrpRevision.SQLWhere = String.Format("MRPRevID = '{0}'", mrpRevID)

        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aMrpRevision, .SecureGrid = True, .Name = "WO List", .FormCtrlCD = MyBase.PanelName}
        aGridControl.ExpandFirstMasterRow = True

        AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateRow
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateRow
        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewRow
        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewRow
        AddHandler aGridControl.AfterViewRegistered, AddressOf AfterViewRegistered

        'Ci-dessous faudra trouver l'equivalent pour le DetailView
        'AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf ProdList_ContextMenuBeforePopup

        aGridControl.GridActionOptionList.Add(New ActionOption("ProdListMenu", "ACT_00138", "Revise", AddressOf ProdListMenu_Revise, True, True, "MRPProdListWithRev_Express"))
        aGridControl.GridActionOptionList.Add(New ActionOption("ProdListMenu", "ACT_00139", "Limit", AddressOf ProdListMenu_Limit, True, True, "MRPProdListWithRev_Express"))
        Me.Cursor = Cursors.Default
    End Sub


    Private Sub WorkOrderExplorer_Load(sender As Object, e As EventArgs) Handles Me.Load
        TreeActionOptionListLoad()
        MetaControlTreeListActionOptionList.ApplySecurity()
    End Sub
End Class
